<?php
/**
 * Base Dao 
 */
require_once 'Zend/Db/Table.php';
class BaseDao extends Zend_Db_Table {
	/**
	 * @var Zend_Db_Adapter_Abstract
	 */
	protected $db;
	
	/**
	 * @var Zend_Db_Adapter_Abstract
	 */
	protected $db_r;
	
	/**
	 * @var Zend_Db_Adapter_Abstract
	 */
	protected $db_w;
	
	public function prefix() {
		return Zend_Registry::get ( "db_table_prefix" );
	}
	
	public function setDb(&$dbs) {
		$this->db = $dbs ['db_w'];
		$this->db_w = $dbs ['db_w'];
		$this->db_r = $dbs ['db_r'];
	}
	
	/**
	 * fetch first one result
	 * @param $sql
	 * @param $para
	 */
	protected function getFirst($sql, $paraArr = array()) {
		if (! empty ( $paraArr )) {
			foreach ( $paraArr as $para ) {
				$sql = $this->db_r->quoteInto ( $sql, $para, null, 1 );
			}
		}
		return $this->db_r->fetchRow ( $sql );
	}
	/**
	 * fetch one
	 * @param $sql
	 * @param $para
	 */
	protected function getOne($sql, $paraArr = array()) {
		if (! empty ( $paraArr )) {
			foreach ( $paraArr as $para ) {
				$sql = $this->db_r->quoteInto ( $sql, $para, null, 1 );
			}
		}
		return $this->db_r->fetchOne ( $sql );
	}
	/**
	 * save a object
	 *
	 * @param $sql
	 * @param $object
	 */
	protected function saveObject($sql, $paraArr) {
		if (! empty ( $paraArr )) {
			foreach ( $paraArr as $para ) {
				$sql = $this->db_w->quoteInto ( $sql, $para, null, 1 );
			}
		}
		$this->db_w->query ( $sql );
		$last_insert_id = $this->db_w->lastInsertId ();
		return $last_insert_id;
	}
	/**
	 * update a object
	 *
	 * @param $tableName
	 * @param $paraArr
	 * @param $where
	 * @return count
	 */
	protected function updateObject($sql, $paraArr) {
		if (! empty ( $paraArr )) {
			foreach ( $paraArr as $para ) {
				$sql = $this->db_w->quoteInto ( $sql, $para, null, 1 );
			}
		}
		return $this->db_w->query ( $sql );
	}
	protected function deleteObject($sql, $paraArr) {
		if (! empty ( $paraArr )) {
			foreach ( $paraArr as $para ) {
				$sql = $this->db_w->quoteInto ( $sql, $para, null, 1 );
			}
		}
		return $this->db_w->query ( $sql );
	}
	
	public function fetchCol($sql, $paraArr) {
		if (! empty ( $paraArr )) {
			foreach ( $paraArr as $para ) {
				$sql = $this->db_r->quoteInto ( $sql, $para, null, 1 );
			}
		}
		return $this->db->fetchCol ( $sql );
	}
	
	public function getPair($sql) {
		return $this->db_r->fetchPairs ( $sql );
	}
	
	public function insertData($data,$withUser = false) {
		$data ['created'] = date ( "Y-m-d H:i:s" );
		$data ['modified'] = date ( "Y-m-d H:i:s" );
		if($withUser == true){
			$loginUser = BaseUtil::getLoginUser();
			if(!empty($loginUser)){				
				$data ['create_id'] = $loginUser['uname'];
				$data ['modify_id'] = $loginUser['uname'];
			}
		}
		return $this->insert ( $data );
	}
	
	public function updateData($data, $where,$withUser = false) {
		$data ['modified'] = date ( "Y-m-d H:i:s" );
	    if($withUser == true){
            $loginUser = BaseUtil::getLoginUser();
            if(!empty($loginUser)){             
                $data ['modify_id'] = $loginUser['uname'];
            }
        }		
		return $this->update ( $data, $where );
	}
	
	public function getByCol($col, $value, $isDel = 0) {
		$select = $this->db_r->select ();
		$select->from ( $this->_name, "*" );
		$select->where ( "$col = ?", $value );
		if ($isDel != - 1) {
			$select->where ( "is_delete = $isDel" );
		}
		$sql = $select->__toString ();
		$result = $this->db_r->fetchRow ( $sql );
		return $result;
	}
}

